package cn.jly.spring.annotation.ioc;

import cn.jly.spring.annotation.ioc.bean.Person;
import cn.jly.spring.annotation.ioc.bean.Student;
import cn.jly.spring.annotation.ioc.config.MainConfigOfPropertyValue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * @author lanyangji
 * @description 关于属性赋值的测试
 * @date 2020/11/13 上午 9:42
 */
public class IocTestOfPropertyValue {

    AnnotationConfigApplicationContext applicationContext;

    @Before
    public void setUp() throws Exception {
        applicationContext = new AnnotationConfigApplicationContext(MainConfigOfPropertyValue.class);
    }

    @After
    public void tearDown() throws Exception {
        applicationContext.close();
    }

    @Test
    public void testValueAnno() {
        final Person person = applicationContext.getBean(Person.class);
        System.out.println("person = " + person);

        final String osName = person.getApplicationContext().getEnvironment().getProperty("os.name");
        System.out.println("osName = " + osName);
    }

    @Test
    public void testProperty() {
        final Student student = applicationContext.getBean(Student.class);
        System.out.println("student = " + student);

        // 配置文件中的配置，可以在环境变量中获取
        final ConfigurableEnvironment environment = applicationContext.getEnvironment();
        final String sName = environment.getProperty("s.name");
        System.out.println("sName = " + sName);
    }
}
